/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Gtk;

namespace Whitemice.ZOGI.GtkForms
{
  public partial class ContactDetailForm : Gtk.Bin, IEntityForm, IStorableEntityForm, IVerifiedEntityForm
  {
		public ContactDetailForm()
		{
			this.Build();
		} /* End constructor */
		
		/* @brief Conveys a date into the birthday field
		   @note If the value is DateTime.MinValue the field is blank */
		public DateTime BirthDate
		{
			get
			{
				if (birthdate.Text.Trim().Length == 0)
					return DateTime.MinValue;
				return Convert.ToDateTime(birthdate.Text);
			}
			set
			{
				if (value == DateTime.MinValue)
					birthdate.Text = "";
				birthdate.Text = value.ToString("yyyy-MM-dd");
			}
		} /* End BirthDate */
		
		/* @brief Checks the format of the date when user leaves the 
		    birthday field.  Birthday field may be a date, or blank */
		protected virtual void OnBirthdateFocusOutEvent(
			object _o, 
			Gtk.FocusOutEventArgs _args)
		{
		  DateTime dateTime = DateTime.MinValue;
			if (birthdate.Text.Trim().Length > 0)
			{
				try 
				{
					dateTime = Convert.ToDateTime(birthdate.Text);
				} finally
					{
					  if (dateTime == DateTime.MinValue)
					  {
					    new MessageBox("The specified date is not valid.", MessageType.Error);
						  ((Entry)_o).GrabFocus();
						}
					}			
			} else BirthDate = DateTime.MinValue;
		} /* End OnBirthdateFocusOutEvent */
		
		/* @brief Set or take the value of the salutation list box 
		   @note The default value is no-salutation-defined */
		public string Salutation
		{
			get
			{
				switch(salutation.Active)
				{
					case 1: return "01_dear_ms";
					case 2: return "02_dear_mrs";
					case 3: return "03_dear_mr";
					case 4: return "04_dear_prof";
					case 5: return "05_dear_dr";
					default: return string.Empty;
				}
			}
			set
			{
				switch(value)
				{
					case "01_dear_ms": salutation.Active = 1; break;
					case "02_dear_mrs": salutation.Active = 2; break;
					case "03_dear_mr": salutation.Active = 3; break;
					case "04_dear_prof": salutation.Active = 4; break;
					case "05_dear_dr": salutation.Active = 5; break;
					default: salutation.Active = 0; break;
				}
			}
		} /* End Salutation */
		
		/* @brief Fill the form from a Contact entity */
		public void Fill(Entity _entity)
		{
			Whitemice.ZOGI.Contact	contact;
			
			if (_entity is Whitemice.ZOGI.Contact)
				contact = (_entity as Whitemice.ZOGI.Contact);
				else throw new Exception("Improper entity type provided");
				
			//this.ZLOG("ContactDetailForm.Fill[{0}]", _entity.ObjectId);
			Sensitive = contact.CanModify;
			lastName.Text = contact.LastName ?? string.Empty;
			middleName.Text = contact.MiddleName ?? string.Empty;
			firstName.Text = contact.FirstName ?? string.Empty;			  
			displayName.Text = contact.DisplayName ?? string.Empty;
			fileAs.Text = contact.FileAs ?? string.Empty;
			managersName.Text = contact.ManagersName ?? string.Empty;
			degree.Text = contact.Degree ?? string.Empty;
			assistantsName.Text = contact.AssistantsName ?? string.Empty;
			department.Text = contact.Department ?? string.Empty;
			url.Text = contact.URL ?? string.Empty;
			office.Text = contact.Office ?? string.Empty;
			occupation.Text = contact.Occupation ?? string.Empty;
			primaryEMail.Text = contact.EMail1 ?? string.Empty;
			secondaryEMail.Text = contact.EMail2 ?? string.Empty;
			tertiaryEMail.Text = contact.EMail3 ?? string.Empty;
			title.Text = contact.Title1 ?? string.Empty;
			secondaryTitle.Text = contact.Title2 ?? string.Empty;
			tertiaryTitle.Text = contact.Title3 ?? string.Empty;
			comment.Buffer.Text = contact.Comment ?? string.Empty;
			BirthDate = contact.BirthDate;
			Salutation = contact.Salutation;			
			switch(contact.Gender)
			{
				case "male": gender.Active = 2; break;
				case "female": gender.Active = 1; break;
				default: gender.Active = 0; break;
			}
			if (contact.IsAccount)
			{
				isAccount.Active = true;
			} else 
			  {
			    isAccount.Active = false;
			  }			 
		} /* End Fill(Entity) */
		
    public void Verify()
    {
      if (lastName.Text.Trim().Length == 0)
        throw new Exception("Contact last name is required.");
    }
    
		public bool Take(Entity _entity)
		{
			Whitemice.ZOGI.Contact	contact;
			
			if (_entity is Whitemice.ZOGI.Contact)
				contact = (_entity as Whitemice.ZOGI.Contact);
				else throw new Exception("Improper entity type provided");

      Console.WriteLine("ContactDetailForm.Take[{0}]", _entity.ObjectId);
			if (contact.CanModify)
			{
				contact.LastName = lastName.Text;
				contact.MiddleName = middleName.Text;
				contact.FirstName = firstName.Text;
				contact.Gender = gender.ActiveText;
				contact.Salutation = Salutation;
				contact.DisplayName = displayName.Text;
				contact.FileAs = fileAs.Text;
				contact.ManagersName = managersName.Text;
				contact.Degree = degree.Text;
				contact.AssistantsName = assistantsName.Text;
				contact.Department = department.Text;
				contact.URL = url.Text;
				contact.Office = office.Text;
				contact.BirthDate = BirthDate;
				contact.Comment = comment.Buffer.Text;
				contact.Occupation = occupation.Text;
				contact.EMail1 = primaryEMail.Text;
				contact.EMail2 = secondaryEMail.Text;
				contact.EMail3 = tertiaryEMail.Text;
				contact.Title1 = title.Text;
				contact.Title2 = secondaryTitle.Text;
				contact.Title3 = tertiaryTitle.Text;
			} else Console.WriteLine("No permissions to modify the Contact!");
      return true;
		} // end Take

    /* @brief Clear the form */
		public void Clear()
		{
			lastName.Text = string.Empty;
			middleName.Text = string.Empty;
			firstName.Text = string.Empty;
			salutation.Active = 0;
			displayName.Text = string.Empty;
			fileAs.Text = string.Empty;
			managersName.Text = string.Empty;
			degree.Text = string.Empty;
			assistantsName.Text = string.Empty;
			department.Text = string.Empty;
			url.Text = string.Empty;
			office.Text = string.Empty;
			birthdate.Text = string.Empty;
			occupation.Text = string.Empty;
			primaryEMail.Text = string.Empty;
			secondaryEMail.Text = string.Empty;
			tertiaryEMail.Text = string.Empty;
			title.Text = string.Empty;
			secondaryTitle.Text = string.Empty;
			tertiaryTitle.Text = string.Empty;
			gender.Active = 0;
			salutation.Active = 0;
		} /* End Clear() */
		
		/* @brief Adjust the sensitive mode of the form */
		public new bool Sensitive
		{
			get { return lastName.Sensitive; }
			set
			{
				lastName.Sensitive = value;
				middleName.Sensitive = value;
				firstName.Sensitive = value;
				gender.Sensitive = value;
				salutation.Sensitive = value;
				displayName.Sensitive = value;
				fileAs.Sensitive = value;
				managersName.Sensitive = value;
				degree.Sensitive = value;
				assistantsName.Sensitive = value;
				department.Sensitive = value;
				url.Sensitive = value;
				office.Sensitive = value;
				birthdate.Sensitive = value;
				occupation.Sensitive = value;
				primaryEMail.Sensitive = value;
				secondaryEMail.Sensitive = value;
				tertiaryEMail.Sensitive = value;
				title.Sensitive = value;
				secondaryTitle.Sensitive = value;
				tertiaryTitle.Sensitive = value;
				comment.Sensitive = value;
			}
		} /* End Sensitive */		
  }
}
